package com.example.administrator.boltshow;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

import application.MyApplication;
import fragment.BoltFragment;
import fragment.HotFragment;
import fragment.MineFragment;
import fragment.PraiseFragment;

public class MainActivity extends AppCompatActivity {

    private static ActionBar mActionBar;
    private RadioGroup mRadioGraoup;
    private Fragment mCurrentFragment;
    private FragmentManager mManager;
    private Timer mTimer;
    private boolean isQuite;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        mTimer = new Timer(true);
        mRadioGraoup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Class<? extends Fragment> clazz = null;
                switch (checkedId) {
                    case R.id.rb_main_bolt:
                        clazz = BoltFragment.class;
                        showAndHide(R.id.fl_main_position, clazz);
                        break;
                    case R.id.rb_main_hot:
                        clazz = HotFragment.class;
                        showAndHide(R.id.fl_main_position, clazz);
                        break;
                    case R.id.rb_main_praise:
                        clazz = PraiseFragment.class;
                        showAndHide(R.id.fl_main_position, clazz);
                        break;
                    case R.id.rb_main_car:
                        MyApplication application = (MyApplication) getApplication();
                        if(application.getIsLogin()) {
                            startActivity(new Intent(MainActivity.this,CarActivity.class));
                        } else {
                            Toast.makeText(MainActivity.this, "请前去登录", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(MainActivity.this,LoginActivity.class));
                        }

                        break;
                    case R.id.rb_main_mine:
                        clazz = MineFragment.class;
                        showAndHide(R.id.fl_main_position, clazz);
                        break;
                }
            }
        });
    }

    private void initView() {
        mRadioGraoup = (RadioGroup) findViewById(R.id.rg_mian_position);
        mManager = getSupportFragmentManager();
        showAndHide(R.id.fl_main_position, BoltFragment.class);
    }
    public void showAndHide(int contentId, Class<? extends Fragment> clazz) {
        FragmentTransaction animation = mManager.beginTransaction();
        //===================
        if (mCurrentFragment != null && mCurrentFragment.getClass().getSimpleName().equals(clazz.getSimpleName())) {

            return;
        }
        Fragment fragment = null;
        Fragment fragmentByTag = mManager.findFragmentByTag(clazz.getSimpleName());
        if (fragmentByTag != null) {
            animation.show(fragmentByTag);
            animation.hide(mCurrentFragment);
            mCurrentFragment = fragmentByTag;


        } else {
            try {
                fragment = clazz.newInstance();
                animation.add(contentId, fragment, clazz.getSimpleName());
                if (mCurrentFragment != null) {
                    animation.hide(mCurrentFragment);


                }
                mCurrentFragment = fragment;
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        animation.commit();
    }

    //改写物理返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(!isQuite) {
                isQuite = true;
                Toast.makeText(MainActivity.this, "在按一次退出闪电降价", Toast.LENGTH_SHORT).show();
                mTimer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        isQuite = false;
                    }
                },3000);
            } else {
                System.exit(0);
            }
        }
        return false;
    }
}
